package com.csweimei.rentmanagementtreasure.tenant.viewModel;

import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.http.Repository;
import com.csweimei.rentmanagementtreasure.http.entity.CustomerBean;
import com.csweimei.rentmanagementtreasure.pop.PopChooseXq;
import com.csweimei.rentmanagementtreasure.pop.PopUtils;
import com.csweimei.rentmanagementtreasure.pop.PopWheelBase;
import com.csweimei.rentmanagementtreasure.tenant.ui.act.VipPersonActivity;
import com.csweimei.rentmanagementtreasure.tenant.ui.act.customer.AddCustomerActivity;
import com.csweimei.rentmanagementtreasure.viewModel.TitleVM;

import java.util.Arrays;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.BR;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class CustomerVm extends TitleVM {
    public ObservableField<String> word = new ObservableField<>();

    public ObservableField<String> px = new ObservableField<>("最近录入");
    public ObservableField<String> strRent = new ObservableField<>("");
    public ObservableField<String> endRent = new ObservableField<>("");
    public ObservableField<String> rent = new ObservableField<>("");
    public ObservableField<String> HouseType = new ObservableField<>("");
    public ObservableField<String> minArea = new ObservableField<>("");
    public ObservableField<String> maxArea = new ObservableField<>("");
    public ObservableField<String> area = new ObservableField<>("");
    public ObservableField<String> toward = new ObservableField<>("");
    public ObservableField<String> other = new ObservableField<>("");
    public ObservableField<String> customType = new ObservableField<>("全部");


    public CustomerVm(Application mApplication, Repository mRepository) {
        super(mApplication, mRepository);
        setTitleText("客户管理");
        setRightIconVisible(View.VISIBLE);
        setRightImg(R.mipmap.tab_one_add);
    }

    @Override
    public void onResume() {
        super.onResume();
        getCustomerData();
    }

    public ObservableList<CustomerItemVM> observableList = new ObservableArrayList<>();
    public ItemBinding<CustomerItemVM> itemBinding = ItemBinding.of(BR.viewModel,R.layout.item_customer);


    int mCusNum = 0;
    private void getCustomerData() {
        Map<String,String> map = AppConfig.getCurMap(model);
        map.put("word",word.get());
        model.seachcustom(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<CustomerBean>() {
                    @Override
                    public void onNext(CustomerBean o) {
                        if (o.issucc){
                            observableList.clear();
                            mCusNum = o.getData().size();
                            for (CustomerBean.DataDTO bean : o.getData()) {
                                CustomerItemVM customerItemVM = new CustomerItemVM(CustomerVm.this,bean);
                                customerItemVM.setCustomerVm(CustomerVm.this);
                                observableList.add(customerItemVM);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    protected void rightIconOnClick() {
        super.rightIconOnClick();
        if (AppConfig.isPersonVip() ||mCusNum <= AppConfig.FREE_NUM ){
            Bundle bundle = new Bundle();
            bundle.putString("flag","add");
            startActivity(AddCustomerActivity.class,bundle);
        }else {
            ToastUtils.showShort("添加客户数已达上限，请前往开通个人专属服务添加更多客户信息。");
            startActivity(VipPersonActivity.class);
        }

    }

    public BindingCommand showPx = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            PopWheelBase popWheelBase = new PopWheelBase(mActivity, 1, new PopWheelBase.Click() {
                @Override
                public void click(String s) {
                    px.set(s);
                    onResume();
                }
            });

            popWheelBase.setList1(Arrays.asList(mActivity.getResources().getStringArray(R.array.customer_type1)));
            PopUtils.showPop(mActivity,popWheelBase,false);
        }
    });

    public BindingCommand showXq = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            PopChooseXq popChooseXq = new PopChooseXq(mActivity, (ysT, hxT, areaT, cxT, otherT) -> {
                rent.set(ysT);
                area.set(areaT);
                HouseType.set(hxT);
                toward.set(cxT);
                other.set(otherT);
                if (ysT.contains("-")){
                    String[] ys = ysT.split("-");
                    strRent.set(ys[0]);
                    endRent.set(ys[1].replace("元",""));
                }else {
                    strRent.set("不限");
                    endRent.set("不限");
                }


                if (areaT.contains("-")){
                    String[] area = areaT.split("-");
                    minArea.set(area[0]);
                    maxArea.set(area[1].replace("㎡",""));
                }else {
                    minArea.set("不限");
                    minArea.set("不限");
                }

                onResume();
            });
            PopUtils.showPop(mActivity,popChooseXq,false);
            popChooseXq.setChoose(rent.get(),HouseType.get(),area.get(),toward.get(),other.get());

        }
    });

    public BindingCommand showMore = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            PopWheelBase popWheelBase = new PopWheelBase(mActivity, 1, new PopWheelBase.Click() {
                @Override
                public void click(String s) {
                    customType.set(s);
                    onResume();
                }
            });

            popWheelBase.setList1(Arrays.asList(mActivity.getResources().getStringArray(R.array.customer_type3)));
            PopUtils.showPop(mActivity,popWheelBase,false);

        }
    });



}
